'''
Created on 2011-7-29

@author: Johnny
'''
from util.inioperator import IniOperator

class Configuration(object):
    '''
    classdocs
    '''


    def __init__(self, file):
        '''
        Constructor
        '''
        self.config_file = file
        self.host_list = []
    
    def config_parser(self, file):
        conf_parser = IniOperator(file)
        return conf_parser
    
    def get_hosts(self, conf_parser):
        '''
        Get test host settings from config.ini.
        '''    
        while len(self.host_list):
            self.host_list.pop(len(self.host_list) - 1)
                        
        opts = conf_parser.cp.options('host')
        for opt in opts:
            self.host_list.append(conf_parser.getValue('host', opt))
            
    def get_db_conf(self, conf_parser):
        '''
        Get result database settings from config.ini.
        ''' 
        self.db_host = conf_parser.getValue('database', 'host')
        self.database = conf_parser.getValue('database', 'database')
        self.db_user = conf_parser.getValue('database', 'user')
        self.db_password = conf_parser.getValue('database', 'password')
        self.table = conf_parser.getValue('database', 'table')
        self.serverlocation = conf_parser.getValue('database', 'location')
        
    def get_email_conf(self, conf_parser):
        '''
        Get email settings from config.ini.
        ''' 
        self.mail_host = conf_parser.getValue('email', 'host')
        self.mail_port = int(conf_parser.getValue('email', 'port'))
        self.mail_user = conf_parser.getValue('email', 'user')
        self.mail_password = conf_parser.getValue('email', 'password')
        self.mail_postfix = conf_parser.getValue('email', 'postfix')
        self.mail_toList = conf_parser.getValue('email', 'tolist').split(',')
        self.mail_subject = conf_parser.getValue('email', 'subject')
        
    def get_rc_conf(self, conf_parser):
        '''
        Get selenium server path and firefox profile path from config.ini.
        '''  
        self.profilepath = conf_parser.getValue('profile', 'profile_path')
        self.serverpath = conf_parser.getValue('server', 'server_path')
        
    def get_import_conf(self, conf_parser):
        '''
        Get import feed settings from config.ini.
        '''   
        self.datapath = conf_parser.getValue('data', 'data_path')
        self.datasheet = conf_parser.getValue('data', 'data_sheet')
        
    def set_url(self, url):
        '''
        Set the test site url
        '''
        conf_parser = IniOperator(self.config_file)
        conf_parser.setValue('urls', 'url', url)
        print 'Configuration -- set url: ' + url
    
    def get_url(self, conf_parser):
        '''
        Get test site
        '''
        self.url = conf_parser.getValue('urls', 'url')
    
    def get_all(self):
        '''
        Get all settngs from config.ini.
        ''' 
        conf_parser = IniOperator(self.config_file)
        self.get_rc_conf(conf_parser)
        self.get_hosts(conf_parser)
        self.get_db_conf(conf_parser)
        self.get_import_conf(conf_parser)
        self.get_email_conf(conf_parser)
        self.get_url(conf_parser)
        

class Host(object):
    '''
    Host related operation.
    '''
    
    browserDict = {       'FireFox': '*firefox',
                'FireFox(high)': '*chrome',
               'FireFox(proxy)': '*pifirefox',
                           'IE': '*iexplore',
                      'IE(hta)': '*iehta',
                    'IE(proxy)': '*piiexplore',
                       'Safari': '*safari',
                'Safari(Proxy)': '*safariproxy',
                       'Chrome': '*googlechrome',
                        'Opera': '*opera',
                       'Custom': '*custom'
              }
    
    def __init__(self):
        '''
        Constructor.
        '''
        self.host = ''
        self.port = ''
        self.browser = ''
    
    def setHost(self, host):
        '''
        Set host.
        Keyword arguments:
        host -- selenium host string
        '''
        self.host = host
        
    def setPort(self, port):
        '''
        Set port.
        Keyword arguments:
        port -- selenium port string
        '''
        self.port = port
        
    def setBrowser(self, browser):
        '''
        Set browser.
        Keyword arguments:
        browser -- browser string
        '''
        self.browser = browser
        
    def setAll(self, string):
        '''
        Set all host related configuration.
        Keyword arguments:
        string -- host, port and browser list
        '''
        print string
        li = string.split(',')
        self.host = li[0]
        self.port = li[1]
        keys = Host.browserDict.keys()
        for key in keys:
            if Host.browserDict[key] == li[2]:
                self.browser = key
                break
    
    def returnString(self):
        '''
        Return host, port and browser string
        '''
        hostString = self.host + ',' + self.port + ',' + \
                     Host.browserDict[self.browser]
        return hostString